//
//  ContentView.swift
//  SwiftUIButton
//
//  Created by 李煜 on 2024/7/28.
//

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack {
            Button {
                print("点击共享按钮")
            } label: {
                Label(
                    title: { Text("Share")
                        .fontWeight(.semibold)
                        .font(.title)
                    },

                    icon: {
                        Image(systemName: "square.and.arrow.up")
                            .font(.title)
                    }
                )
            }
            // 自定义按钮样式并应用
            .buttonStyle(MyGradientBackgroundStyle())

            Button {
                print("点击编辑按钮")
            } label: {
                Label(
                    title: { Text("Edit")
                        .fontWeight(.semibold)
                        .font(.title)
                    },

                    icon: {
                        Image(systemName: "square.and.pencil")
                            .font(.title)
                    }
                )
            }
            .buttonStyle(MyGradientBackgroundStyle())

            Button {
                print("点击删除按钮")
            } label: {
                Label(
                    title: { Text("Delete")
                        .fontWeight(.semibold)
                        .font(.title)
                    },

                    icon: {
                        Image(systemName: "trash")
                            .font(.title)
                    }
                )
            }
            .buttonStyle(MyGradientBackgroundStyle())

            // 作业 : 一个按下会顺时针旋转45°的icon的按钮
            Button {
                print("点击了旋转按钮")
            } label: {
                Image(systemName: "plus")
            }
            .buttonStyle(RotationButtonStyle())
			
			
			//iOS 15 API
			Button(action: {
				
			}, label: {
				Text("Buy me a coffee")
			})
			.tint(.purple)
			.buttonStyle(.bordered)
			//圆角矩形
//			.buttonBorderShape(.roundedRectangle(radius: 5))
			//胶囊
			.buttonBorderShape(.capsule)
			.controlSize(.large)
        }
    }
}

// 创建一个按钮样式
struct MyGradientBackgroundStyle: ButtonStyle {
    func makeBody(configuration: Configuration) -> some View {
        configuration.label
            .frame(minWidth: /*@START_MENU_TOKEN@*/0/*@END_MENU_TOKEN@*/, maxWidth: .infinity)
            .padding()
            .foregroundColor(.white)
            .background(LinearGradient(colors: [.darkGreen, .lightGreen], startPoint: .leading, endPoint: .trailing))
            .cornerRadius(40)
            .padding(.horizontal, 20)
            .scaleEffect(configuration.isPressed ? 0.9 : 1.0)
    }
}

// 作业
struct RotationButtonStyle: ButtonStyle {
    func makeBody(configuration: Configuration) -> some View {
        configuration.label

            .padding()
            .background(.purple)
            .foregroundColor(.white)
            .clipShape(Circle())
            .rotationEffect(configuration.isPressed ? .degrees(45.0) : .degrees(0))
    }
}

#Preview {
    ContentView()
}
